package copyengine.ui.infoTree.loadTexture
{
    import copyengine.infoTree.meta.CESFileMeta;
    import copyengine.resource.CEResUtils;
    import copyengine.resource.resInfo.CEResInfoManger;
    import copyengine.resource.resInfo.MetaEachResFileInfo;
    import copyengine.resource.resProxy.CEResFIleDesc;
    import copyengine.resource.resProxy.CEResProxy;
    import copyengine.resource.resProxy.loadQueue.CEResLoadQueue;
    import copyengine.utils.CECallbackFunction;
    import copyengine.utils.CEMemoryHolder;
    import copyengine.utils.CEStringUtils;

    public class ITTextureLoadHelper
    {
        private var mFileName:String;
        private var mSymbolName:String;
        private var mResProxy:CEResProxy;
        private var mFinishCallback:CECallbackFunction;
        private var mAllNeedLoadAtlasArray:Array;

        public function ITTextureLoadHelper(_fileName:String, _symbolName:String, _resProxy:CEResProxy, _finishCallback:CECallbackFunction) {
            CEMemoryHolder.instance.holdItem(this);

            mFileName = _fileName;
            mSymbolName = _symbolName;
            mResProxy = _resProxy;
            mFinishCallback = _finishCallback;

            start();
        }

        private function start():void {

            var resQueue:CEResLoadQueue = new CEResLoadQueue(onCEFileLoadComplete);
            CEResUtils.pushFileToNeedLoadResVector(mFileName, CEResFIleDesc.FILE_TYPE_COMPRESSED_OBJ, resQueue);
            mResProxy.loadResQueue(resQueue);
        }

        private function onCEFileLoadComplete():void {
            var ceFile:CESFileMeta = CEResUtils.getDecompressedObj(mFileName, mResProxy);
            if (ceFile.isShareTexture) {
                mAllNeedLoadAtlasArray = ceFile.needAtlasNameDic[ceFile.fileName];
            } else {
                mAllNeedLoadAtlasArray = ceFile.needAtlasNameDic[mSymbolName];
            }

            if (mAllNeedLoadAtlasArray.length > 0) {

                var resQueue:CEResLoadQueue = new CEResLoadQueue(finish);

                for each(var needLoadAtlasName:String in mAllNeedLoadAtlasArray) {

                    //===================//
                    //== 注意! 目前假设所有的atlas 结尾为xml,对应的纹理图为同名文件 结尾为png
                    //== 当后续使用ATF 或者atlas与png不同名时候再更改加载逻辑
                    //====================
                    var atlasFileInfo:MetaEachResFileInfo = CEResInfoManger.instance.getFileInfoByFileName(needLoadAtlasName);
                    var pngFileInfo:MetaEachResFileInfo = CEResInfoManger.instance.getFileInfoByFileName(
                            CEStringUtils.beforeLast(needLoadAtlasName, ".xml") + ".png");

                    var fileDesc:CEResFIleDesc = new CEResFIleDesc();
                    fileDesc.fileName = pngFileInfo.name;
                    fileDesc.filePath = pngFileInfo.path;
                    fileDesc.fileSize = pngFileInfo.size;
                    fileDesc.fileType = CEResFIleDesc.FILE_TYPE_ATLAS_TEXTURE;
                    fileDesc.extData = {};
                    fileDesc.extData[CEResFIleDesc.EXT_ATLAS_XML_FILE_PATH] = atlasFileInfo.path;
                    resQueue.pushNeedLoadRes(fileDesc);
                }
                mResProxy.loadResQueue(resQueue);
            }
            else {
                finish();
            }

        }

        //=======================//
        //== Finish
        //=======================//
        private function finish():void {
            mFinishCallback.isEnable && mFinishCallback.callback();
            CEMemoryHolder.instance.releaseItem(this);
            mFinishCallback = null;
            mResProxy = null;
        }

    }
}
